﻿using System.IO;
using System.Reflection;

namespace Builderator.Core
{
    public class PluginManager
    {
        private LocalLoader localLoader;

        public string PluginDirectory
        {
            get;
            set;
        }

        public string[] GetTypes
        {
            get
            {
                return this.localLoader.Types;
            }
        }

        public PluginManager(string relativePath)
        {
            string assemblyLocation, currentDirectory;

            assemblyLocation = Assembly.GetExecutingAssembly().Location;

            currentDirectory = assemblyLocation.Substring(0, assemblyLocation.LastIndexOf(Path.DirectorySeparatorChar) + 1);

            this.PluginDirectory = Path.Combine(currentDirectory, relativePath);

            if (!this.PluginDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                this.PluginDirectory += Path.DirectorySeparatorChar;
            }

            this.localLoader = new LocalLoader(this.PluginDirectory);
        }

        public void LoadAssembly(string assemblyFullName)
        {
            this.localLoader.LoadAssembly(assemblyFullName);
        }

        public object CreateInstance(string assemblyName)
        {
            return this.localLoader.CreateInstance(assemblyName);
        }
    }
}